package com.tudulist.web;

import static com.tudulist.web.utils.TudulistUtil.TODOLISTID;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;

import com.tudulist.model.TodoList;
import com.tudulist.rest.resource.TodoListResource;
import com.tudulist.rest.resource.UserResource;
import com.tudulist.web.utils.TudulistUtil;

@SuppressWarnings("serial")
public class TudulistHomePage extends WebPage {

	@SpringBean(name = "todoListResourceClient")
	TodoListResource todoListResourceClient;

	@SpringBean
	UserResource userResourceClient;

	private FeedbackPanel feedBackpanel;

	private WebMarkupContainer latestTodolistContainer;

	@Override
	protected void onInitialize() {
		super.onInitialize();
		buildForm();
		buildFeedBack();
		buildUserProfileLink();
		buildLatestTodoList();
	}

	/**
	 * display the latest todo list
	 */
	private void buildLatestTodoList() {
		latestTodolistContainer = new WebMarkupContainer("latestTodolistContainer");

		ListView<TodoList> latestTodoListView = new ListView<TodoList>("latestTodoListView", new PropertyModel<List<? extends TodoList>>(this,
				"todolists")) {
			@Override
			protected void populateItem(final ListItem<TodoList> item) {
				Link<TodoList> todoListLink = new Link<TodoList>("todoListLink", item.getModel()) {

					@Override
					public void onClick() {
						PageParameters parameters = new PageParameters();
						parameters.add(TODOLISTID, item.getModel().getObject().getId());
						setResponsePage(TodoPage.class, parameters);
					}
				};
				todoListLink.add(new Label("todoListLabel", item.getModel().getObject().getName()));
				item.add(todoListLink);

			}

		};
		latestTodoListView.setOutputMarkupId(true);
		latestTodolistContainer.setOutputMarkupId(true);
		latestTodolistContainer.add(latestTodoListView);
		add(latestTodolistContainer);
	}

	/**
	 * logout link
	 */
	private void buildUserProfileLink() {
		// FIXME share security context
		add(new Label("username", userResourceClient.getCurrentUser(TudulistUtil.getCurrentUserUsername()).getFirstName() + " "
				+ userResourceClient.getCurrentUser(TudulistUtil.getCurrentUserUsername()).getLastName()));
		add(new Link<Void>("logout") {

			@Override
			public void onClick() {
				TudulistUtil.logout();
			}

		});

	}

	/**
	 * form feedback panel
	 */
	private void buildFeedBack() {
		feedBackpanel = new FeedbackPanel("feedback");
		feedBackpanel.setOutputMarkupId(true);
		add(feedBackpanel);

	}

	/**
	 * a form to create a new todo list
	 */
	private void buildForm() {
		final TextField<String> name = new TextField<String>("name", new Model<String>("insert a name"));
		Form<Void> form = new Form<Void>("form");
		form.add(name);
		form.add(new AjaxSubmitLink("submit") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				TodoList todoList = new TodoList();
				todoList.setLastUpdate(new DateTime());
				todoList.setName(name.getModelObject());
				// FIXME share security context
				todoListResourceClient.createTodoList(TudulistUtil.getCurrentUserUsername(), todoList);
				info("Todo list created with success.");
				target.add(feedBackpanel);
				target.add(latestTodolistContainer);
			}
		});
		add(form);
	}

	public List<TodoList> getTodolists() {
		return todoListResourceClient.findAll(TudulistUtil.getCurrentUserUsername());
	}

}
